package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.SugestionDTO;
import com.lastproject.ejb.entities.Sugestion;
import com.lastproject.ejb.entities.User;

/**
 * Session Bean implementation class SugestionConversion
 */
@Stateless
@LocalBean
public class SugestionConversion {

	@PersistenceContext
	EntityManager em;

	public SugestionConversion() {
	}

	public SugestionDTO fromEntity(Sugestion s) {
		SugestionDTO sugestion = new SugestionDTO();
		sugestion.setId(s.getId());
		sugestion.setSubject(s.getSubject());
		sugestion.setMessage(s.getMessage());
		sugestion.setUserId(s.getUser().getId());
		sugestion.setUserName(s.getUser().getFirstname() + " "
				+ s.getUser().getLastname());
		return sugestion;
	}

	public Sugestion fromDTO(SugestionDTO s) {
		Sugestion sugestion = em.find(Sugestion.class, s.getId());
		if (sugestion == null)
			sugestion = new Sugestion();
		sugestion.setSubject(s.getSubject());
		sugestion.setMessage(s.getMessage());
		sugestion.setUser(em.find(User.class, s.getUserId()));
		return sugestion;
	}

}
